package com.lagou.edu.utils;

import java.sql.SQLException;

/**
 * 事务管理者
 */
public class TransactionManger {

    //创建对象交给Ioc容器
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    /*
    //构造函数私有化
    private TransactionManger() {
    }

    //初始化
    private static TransactionManger transactionManger = new TransactionManger();

    //提供对外获取对象的接口
    public static TransactionManger getInstance() {
        return transactionManger;
    }*/

    /**
     * 关闭数据库连接自动提交事务
     *
     * @throws SQLException
     */
    public void closeAutoCommit() throws SQLException {
        connectionUtils.getConnection().setAutoCommit(false);
    }

    /**
     * 提交事务
     *
     * @throws SQLException
     */
    public void commit() throws SQLException {
        connectionUtils.getConnection().commit();
    }

    /**
     * 回滚事务
     *
     * @throws SQLException
     */
    public void rollback() throws SQLException {
        connectionUtils.getConnection().rollback();
    }
}
